# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


from scrapy.pipelines.images import ImagesPipeline
from scrapy.http import Request
from scrapy.exceptions import DropItem

class WallhavenWallpaperPipeline(ImagesPipeline):
    """下载壁纸的Pipeline"""


    def file_path(self, request, response=None, info=None):
        """这边将文件放在如 ‘SFW/wallhaven-xxxxxx.png’ 的位置 """
        url  = request.url
        name = url.split('/')[-1]
        cat  = request.meta['flag']
        return cat + '/' + name


    def get_media_requests(self, item, info):
        """获取图片的URL，生成Request"""
        return [Request(url=item['url'], meta=item)]
        

    def item_completed(self, results, item, info):
        """图片下载完成，判断是否下载成功，记录保存位置"""
        image_path = [x['path'] for ok,x in results if ok]
        if not image_path:
            raise DropItem("Image Download Failed!!")
        item['local_path'] = image_path
        return item